<!-- 合同管理->渠道/经纪管理->新增/编辑/查看 -->
<template>
  <div class="channel-box">
    <el-breadcrumb separator="/" class="bread-title">
      <el-breadcrumb-item :to="{ path: '/channelOrAgentManage' }">渠道经纪管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{pageType}}</el-breadcrumb-item>
    </el-breadcrumb>
    <formTtitle class="info-content" title="基本信息">
      <basicInfo
        :typeForm="typeForm"
        :necessary="necessary"
        :typeStatus="typeStatus"
        ref="basicInfo"
      ></basicInfo>
    </formTtitle>
    <formTtitle class="info-content" title="账户信息">
      <accountInfo
        :typeForm="typeForm"
        :necessary="necessary"
        :typeStatus="typeStatus"
        ref="accountInfo"
      ></accountInfo>
    </formTtitle>
    <formTtitle class="info-content" title="业务信息">
      <businessInfo
        :typeForm="typeForm"
        :necessary="necessary"
        :typeStatus="typeStatus"
        ref="businessInfo"
      ></businessInfo>
    </formTtitle>
    <formTtitle class="info-content" title="附件信息">
      <fileInfo :typeForm="typeForm" :typeStatus="typeStatus" :necessary="necessary" ref="fileInfo"></fileInfo>
    </formTtitle>

    <!-- 联系人信息 -->
    <contacts-table
      v-show="!typeForm.sort == '1'"
      :typeStatus="typeStatus"
      :insideId="insideId"
      :typeId="1"
      ref="contactsTable"
    ></contacts-table>

    <p v-if="typeStatus !='view'" class="txc-cn">
      <el-button type="primary" @click="loadApproval">发起审批</el-button>
      <el-button type="primary" v-if="typeStatus =='add'" @click="addSave(0)">保存</el-button>
      <el-button type="primary" v-if="typeStatus =='edit'" @click="editSave(0)">保存</el-button>
      <el-button type="primary" @click="back">取消</el-button>
    </p>
    <div v-else>
      <!-- 审核记录 -> 详情页面显示 -->
      <audit-table :tableData="auditData"></audit-table>
      <p class="txc-cn">
        <el-button type="primary" @click="back">返回</el-button>
      </p>
    </div>
  </div>
</template>

<script>
import api from "@/api/apiPath";
import utils from "@/common/utils";
import auditTable from "../../common/auditTable"; //审核记录表
import contactsTable from "../../common/contactsTable"; //联系人信息表

import formTtitle from "./formTitle";
import basicInfo from "./basicInfo"; //基本细腻些
import accountInfo from "./accountInfo"; //账户星系
import businessInfo from "./businessInfo"; //业务信息
import fileInfo from "./fileInfo"; //附件信息
export default {
  components: {
    "contacts-table": contactsTable,
    "audit-table": auditTable,

    formTtitle,
    basicInfo,
    accountInfo,
    businessInfo,
    fileInfo
  },
  data() {
    return {
      necessary: false, //是否需要审核验证
      bankShow: false,
      auditData: [],
      typeForm: {
        sort: 0,
        companyRepresentative: "",
        bankName: "",
        bankAccount: "",
        companyName: "",
        bankAreaCode: "",
        companyTaxpayer: "",
        openingBank: "",
        bankNumber: "",
        companyProvince: "",
        companyCity: "",
        companyCounty: "",
        companyCityCode: "",
        companyAddress: "",
        openingBankProvince: "",
        openingBankCity: "",
        companyPhone: "",
        companyPostalcode: "",
        apply: "",
        openingBankLicense: "",
        businessLicense: "",
        idCardFront: "",
        idCardBack: "",

        house: -1,
        insurance: -1,
        property: -1,
        taxrate: "3"
      },

      backCityOptions: [], //城市
      provinceOptions: [], //省份
      cityOptions: [], //城市
      areaOptions: [], //区
      typeStatus: "",
      pageType: "",
      insideId: "",
      sort: "",
      typeFunction: {
        //页面状态
        add: () => {
          this.pageType = "新增";
        },
        edit: (insideId, sort) => {
          this.pageType = "编辑";
          this.loadFromDetail(insideId, sort); //加载表单信息
        },
        view: (insideId, sort) => {
          this.pageType = "详情";
          this.loadFromDetail(insideId, sort);
        }
      }
    };
  },
  methods: {
    async veri() {
      let _this = this;
      let basicInfoVeri = await this.$refs["basicInfo"].veri();
      let accountInfoVeri = await this.$refs["accountInfo"].veri();
      let businessInfoVeri = await this.$refs["businessInfo"].veri();
      let fileInfoVeri = await this.$refs["fileInfo"].veri();
      let fileUploadFlag = true;
      let veriFlag =
        basicInfoVeri && accountInfoVeri && businessInfoVeri && fileInfoVeri;

      if (veriFlag) {
        //验证通过上传图片
        fileUploadFlag = await this.$refs["fileInfo"].submit();
      }
      //关闭校验，避免无法保存
      this.necessary = false;
      return veriFlag && fileUploadFlag;
    },
    async addSave(status) {
      let contactList = this.$refs["contactsTable"].returnTabel();
      let flag = await this.veri(1);

      if (!flag) return;
      let params = {
        channel: this.typeForm,
        contact: contactList,
        examineStatus: status
      };
      this.axios.post(`${api.ADDCHANNELCOMPANY}`, params).then(res => {
        if (res.data.code == 200) {
          this.$router.go(-1);
          this.$message({
            type: "success",
            message: status == 0 ? "添加成功" : "已发起审核"
          });
        }
      });
    },
    async editSave(status) {
      let flag = await this.veri();
      if (!flag) return;
      this.typeForm.examineStatus = status;
      this.axios.post(`${api.EDITCHANNELMPANY}`, this.typeForm).then(res => {
        if (res.data.code == 200) {
          this.$router.go(-1);
          this.$message({
            type: "success",
            message: status == 0 ? "编辑成功" : "已发起审核"
          });
        }
      });
    },
    back() {
      this.$router.go(-1);
    },
    //加载当前页面表单详情
    loadFromDetail(insideId, sort) {
      let _this = this;
      this.axios
        .post(`${api.CHANNELCOMPANYDETAIL}`, { id: insideId, sort: sort })
        .then(res => {
          if (res.data.code == 200) {
            _this.typeForm = res.data.data.channelAll || {};
            let urlParams = [
              "openingBankLicense",
              "businessLicense",
              "idCardFront",
              "idCardBack",
              "strategicAgreement",
              "consentletter"
            ];
            urlParams.forEach(url => {
              _this.typeForm[url] = this.formatImgUrl(_this.typeForm[url]);
            });
            _this.contactTAbleData = res.data.data.contactList;
            _this.auditData = res.data.data.operation;
          }
        });
    },
    //格式化附件路径
    formatImgUrl(url) {
      if (!url) {
        return "";
      }
      return url.split(";")[0];
    },
    //发起审批
    loadApproval() {
      //发起审批开启强校验
      this.necessary = true;
      this.$nextTick(() => {
        if (this.typeStatus == "add") {
          this.addSave(1);
        } else {
          this.editSave(1);
        }
      });
    }
  },
  created() {
    let { pageType, insideId, sort } = this.$route.query;
    //更改页面状态（新增/编辑/查看）
    this.typeStatus = pageType;
    this.insideId = insideId;
    this.sort = sort;
    //执行该状态init()
    this.typeFunction[pageType](insideId, sort);
  },
  destroyed() {},
  filters: {}
};
</script>

<style lang="stylus" >
.channel-box {
  .txc-cn {
    text-align: center;
  }

  p {
    margin: 0;
    padding: 20px 0;
  }

  .type-item-title {
    font-size: 18px;
    color: #303133;
    font-weight: 500;
    padding-bottom: 20px;
    margin-bottom: 20px;
    border-bottom: 1px solid #ddd;
    margin-top: 40px;
  }
}
</style>
